<div class="layui-form layui-field-box">
    <div class="layui-form-item" style="margin:0;margin-top:15px;">
        <div class="layui-inline">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-inline layui-short-input">
                <input type="text" placeholder="标题" name="{{makeElUniqueName('title')}}" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">类型</label>
            <div class="layui-input-inline layui-short-input">
                <select name="{{makeElUniqueName('type')}}">
                    <option value="0">(请选择新闻类型)</option>
                    @foreach(\App\News::$NEWS_TYPE as $item)
                        <option value='{{$item['key']}}'>{{$item['text']}}</option>
                    @endforeach
                </select>
            </div>
            <div class="layui-input-inline" style="width:auto">
                <button class="layui-btn" lay-filter="{{makeElUniqueName('search_group')}}"><i class="layui-icon">&#xe615;</i> 搜索</button>&nbsp;
                <button class="layui-btn layui-btn-normal" lay-filter="{{makeElUniqueName('add_group')}}"><i class="layui-icon">&#xe654; </i> 新增</button>
            </div>
        </div>
    </div>
</div>
<div id="{{makeElUniqueName('tbSimpleUserGroup')}}"></div>
<script>
    layui.use(['jfTable', 'form', 'addressUtil'], function(){
        var layer = layui.layer;
        var $ = layui.jquery;
        var jfTable = layui.jfTable;
        var form = layui.form();
        var addressUtil = layui.addressUtil;

        form.render();

        addressUtil.twoLevelAddressSelect({
            provinceSelect: $('select[name=\'{{makeElUniqueName('group_province')}}\']'),
            citySelect: $('select[name=\'{{makeElUniqueName('group_city')}}\']'),
        });

        layui.define(function(exports){
            var obj = {
                doEdit:function(partnerId) {
                    $.get('/operations/news/edit/'+ partnerId, {}, function(str){
                        var popLayerUtil = layui.popLayerUtil;
                        popLayerUtil.doPopUp({
                            index: layer.open({
                                id: '{{makeElUniqueName('editSimpleGroup')}}',
                                title: '修改新闻',
                                type: 1,
                                content: str,
                                area: ['950px', '600px']
                            }),
                            onClose: function() {
                                layui.userGroupsFuncs.refreshTableGrid();
                            }
                        });
                    });
                },
                doDelete:function(partnerId) {
                    layer.confirm('确定删除该新闻？', {
                        btn: ['确定','放弃'],
                        icon: 3
                    }, function(){
                        var index = layer.load(1);
                        $.ajax({
                            contentType: "application/json",
                            type: 'post',
                            url: '/backstage/api/news/delete',
                            data: JSON.stringify({
                                id: partnerId
                            }),
                            success: function (outResult) {
                                layer.close(index);
                                if (outResult.Success) {
                                    layer.msg(outResult.Message, { icon: 6 });
                                    layui.userGroupsFuncs.refreshTableGrid();
                                } else {
                                    layer.msg(outResult.Message, { icon: 5 });
                                }
                            },
                            error: function (error) {
                                layer.close(index);
                                layui.validator.processValidateError(error);
                            }
                        });
                    }, function(){
                    });
                },
                refreshTableGrid: function() {
                    $('input[name=\'{{makeElUniqueName('title')}}\']').val('');
                    $('select[name=\'{{makeElUniqueName('type')}}\']').val('');
                    $("#{{makeElUniqueName('tbSimpleUserGroup')}}").jfTable("reload");
                }
            };
            exports('userGroupsFuncs', obj);
        });

        $("#{{makeElUniqueName('tbSimpleUserGroup')}}").jfTable({
            url: '/backstage/api/news/query',
            pageSize:5,
            page: true,
            skip: true,
            first:'首页',
            last:'尾页',
            columns: [{
                text:'操作',
                name: 'id',
                width: 200,
                align: 'center',
                formatter: function(value, dataItem, index) {
                    var html = '<a class="layui-btn layui-btn-small layui-btn-normal" onclick="layui.userGroupsFuncs.doEdit(' + value + ')"><i class="layui-icon">&#xe642;</i> 编辑</a>';
                    html += '&nbsp;&nbsp;<a class="layui-btn layui-btn-small layui-btn-danger" onclick="layui.userGroupsFuncs.doDelete(' + value + ')"><i class="layui-icon">&#xe640;</i> 删除</a>';
                    return html;
                }
            },{
                text:'新闻标题',
                name: 'title',
                width: 380,
                align: 'center',
            },{
                text:'新闻类型',
                name: 'type',
                width: 240,
                align: 'center',
                formatter:function(value, dataItem, index) {
                    if (dataItem.type == {{\App\News::$NEWS_TYPE_ZCDT}}) {
                        return '<span style="color:red">政策动态</span>';
                    } else if (dataItem.type == {{\App\News::$NEWS_TYPE_ZYDJ}}) {
                        return '<span style="color:blue">志愿党建</span>';
                    } else if (dataItem.type == {{\App\News::$NEWS_TYPE_BDDT}}) {
                        return '<span style="color:gray">本地动态</span>';
                    }
                    return value;
                }
            },{
                text:'新闻图片',
                name: 'getFullIconPath',
                width: 240,
                align: 'center',
                formatter:function(value, dataItem, index) {
                    return '<img src=\'' + value + '\' width=40 height=40></img>';
                }
            }],
            method: 'get',
            queryParam: {
                title:$('input[name=\'{{makeElUniqueName('title')}}\']').val(),
                type:$('select[name=\'{{makeElUniqueName('type')}}\']').val(),
            },
            toolbarClass: 'layui-btn-small',
            onBeforeLoad: function (param) {
                return $.extend(param, {
                    title:$('input[name=\'{{makeElUniqueName('title')}}\']').val(),
                    type:$('select[name=\'{{makeElUniqueName('type')}}\']').val(),
                });
            },
            onLoadSuccess: function (data) {
                return data;
            },
            dataFilter:function (data) {
                return data;
            }
        });

        $('.layui-btn[lay-filter=\'{{makeElUniqueName('search_group')}}\']').on('click', function(){
            $("#{{makeElUniqueName('tbSimpleUserGroup')}}").jfTable("reload");
        });

        $('.layui-btn[lay-filter=\'{{makeElUniqueName('add_group')}}\']').on('click', function(){
            $.get('/operations/news/create', {}, function(str){
                var popLayerUtil = layui.popLayerUtil;
                popLayerUtil.doPopUp({
                    index: layer.open({
                        id: '{{makeElUniqueName('createSimpleGroup')}}',
                        title: '新建新闻',
                        type: 1,
                        content: str,
                        area: ['950px', '600px']
                    }),
                    onClose: function() {
                        layui.userGroupsFuncs.refreshTableGrid();
                    }
                });
            });
        });
    });
</script>
